package com.susallwave.srcb.controller.sys.rbac;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.susallwave.srcb.dto.PermissionTreeNode;
import com.susallwave.srcb.entity.sys.Permission;
import com.susallwave.srcb.response.Result;
import com.susallwave.srcb.service.sys.PermissionService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/permission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "权限管理", description = "权限管理")
public class PermissionController {

    private final PermissionService permissionService;

    @GetMapping("/page")
    public Result<IPage<Permission>> page(@RequestParam(defaultValue = "1") int pageNum,
                                          @RequestParam(defaultValue = "10") int pageSize,
                                          @RequestParam(required = false) String code,
                                          @RequestParam(required = false)String name,
                                          @RequestParam(required = false)String status) {
        return permissionService.page(pageNum,pageSize,code,name,status);
    }

    @PostMapping("/add")
    public Result<Void> add(@RequestBody Permission permission) {
        return permissionService.add(permission);
    }

    @PostMapping("/edit")
    public Result<Void> edit(@RequestBody Permission permission) {
        return permissionService.edit(permission);
    }

    @DeleteMapping("/delete")
    public Result<String> delete(@RequestBody List<Long> ids) {
        permissionService.removeByIds(ids);
        return Result.success("删除成功");
    }

    @GetMapping("/tree")
    public Result<List<PermissionTreeNode>> tree() {
        return Result.success(permissionService.listPermissionTree());
    }

    @GetMapping("/tree/with-checked")
    public Result<Map<String, Object>> treeWithChecked(@RequestParam Long roleId) {
        return Result.success(permissionService.listPermissionTreeByRoleId(roleId));
    }

    @GetMapping("/detail")
    public Result<Permission> detail(@RequestParam Long id) {
        return Result.success(permissionService.getById(id));
    }

    @GetMapping("/validate")
    public Result<Void> validate(@RequestParam(required = false) String code,
                                 @RequestParam(required = false) String name) {
        return permissionService.isRepeat(code,name);
    }

    @GetMapping("/list-by-type")
    public Result<List<Permission>> listByType(@RequestParam String type) {
        return Result.success(permissionService.listByType(type));
    }
}
